<?php
namespace app\admin\controller;
use think\Controller;


class Common extends Controller
{
    protected $_visit_action;
    protected $_visit_controller;
    protected $_login_error_text;

    public function _initialize() {
        $request = request();
        $this->_visit_action = $request->action();
        $this->_visit_controller = strtolower($request->controller());
        $this->assign('_login_error_state', 0); //默认输出登录成功
        $this->assign('tmp_path', $this->_visit_controller . '/' . $this->_visit_action); //输出当前控制器+方法  用于前台包含对应的模版文件

        if(!$now_menu = $this->checkLogon()) {
            $this->assign('_login_error_state', 1);
            $this->assign('_login_error', $this->_login_error_text);
        }

        $menu = $this->getMenu();
        $this->assign('menu', $menu); //输出页面导航
        $this->assign('now_menu', $now_menu); //输出页面导航

        $crumbs = $this->getCrumbs();
        $this->assign('crumbs', $crumbs); //输出页面导航

    }

    public function checkLogon()
    {
        if(session('admin_login') == 1) {
            //验证权限
            if(!$now_menu = $this->checkJurisdiction()) {
                $this->_login_error_text = '你没有访问权限，请联系系统管理员';
                return false;
            }
            return $now_menu;
        } else {
            $this->redirect(url('login/index'));
            return false;
        }
    }

    /* 检查用户权限 */
    public function checkJurisdiction() {
        $menu = config('menu');
        $path = $this->_visit_controller . '/' . $this->_visit_action;
        $now_menu = false;
        foreach($menu as $mm => $m) {
            foreach($m['children'] as $k => $v) {
                if($v['path'] == $path) {
                    $now_menu = $v;
                    break;
                }
            }
            if($now_menu) {
                break;
            }
        }
        return $now_menu;
//        if($now_menu && $admin_jurisdiction = session('admin_jurisdiction_' . session('admin_id'))) {
//            if(in_array($k, $admin_jurisdiction)) {
//                return $now_menu;
//            }
//        }
//        return false;
    }

    /* 获取后台导航 */
    public function getMenu() {
        if( !$menu = cache('menu')) {
            $menu = config('menu');
            /* 过滤掉不需要显示的数据 */
            foreach($menu as $mm => $m) {
                foreach($m['children'] as $k => $v) {
                    if($v['is_show'] == 0) {
                        unset($menu[$mm]['children'][$k]);
                    }
                }
            }
            cache('menu', $menu);
        }
        return $menu;
    }

    /* 获取面包屑 */
    public function getCrumbs() {
        $menu = config('menu');
        $path = $this->_visit_controller . '/' . $this->_visit_action;
        $crumbs = false;
        $children_name = '';
        $menu_name = '';
        foreach($menu as $mm => $m) {
            foreach($m['children'] as $k => $v) {
                if($v['path'] == $path) {
                    $children_name = $v['name'];
                    break;
                }
            }
            if($children_name) {
                $menu_name = $m['name'];
                break;
            }
        }

        $crumbs = '<li>'.
                '<a href="/">首页</a>'.
                '</li>'.
                '<li>'.
                '<a>' . $menu_name . '</a>'.
                '</li>'.
                '<li class="active">'.
                '<strong>' . $children_name . '</strong>'.
                '</li>';
        return $crumbs;
    }

    // 上传图片
    public function upload_image() {
        $type = input('param.type');
        $files = request()->file('file');
        if ($files) {
            foreach($files as $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . $type);
                if ($info) {
                    $image = $info->getSaveName();
                    return json(['code' => 200, 'data' => ['path' => $image]]);
                } else {
                    // 上传失败获取错误信息
                    return json(['code' => 0, 'msg' => $file->getError()]);
                }
            }

        }
        return json(['code' => 0, 'msg' => '请选择要上传的文件']);
    }
}
